home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / delphi.swg / 0012_Resizing and dividing panels.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-11-22  |  2.6 KB  |  103 lines

  1.  
  2. {
  3. Here's the source code for a resizable panel.  Give the panel an align
  4. property of alClient, throw some controls on it, and watch them resize
  5. at run time when you resize the form.  There is some code that prohibits
  6. resizing during design time, but this can be taken out.  This may not be
  7. perfect, because I threw it together in a few minutes, but it's worked
  8. for me so far.
  9. }
  10.  
  11. unit Elastic;
  12.  
  13. interface
  14.  
  15. uses
  16.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  17.   Forms, Dialogs, ExtCtrls;
  18.  
  19. type
  20.   TElasticPanel = class( TPanel )
  21.   private
  22.      FHorz, FVert: boolean;
  23.      nOldWidth, nOldHeight: integer;
  24.      bResized: boolean;
  25.   protected
  26.      procedure WMSize( var message: TWMSize ); message WM_SIZE;
  27.   public
  28.      nCount: integer;
  29.      constructor Create( AOwner: TComponent ); override;
  30.   published
  31.      property ElasticHorizontal: boolean read FHorz write FHorz default 
  32. TRUE;
  33.      property ElasticVertical: boolean read FVert write FVert default 
  34. TRUE;
  35.   end;
  36.  
  37. procedure Register;
  38.  
  39. implementation
  40.  
  41. constructor TElasticPanel.Create( AOwner: TComponent );
  42. begin
  43.   inherited Create( AOwner );
  44.   FHorz := TRUE;
  45.   FVert := TRUE;
  46.   nOldWidth := Width;
  47.   nOldHeight := Height;
  48.   bResized := FALSE;
  49. end;
  50.  
  51. procedure TElasticPanel.WMSize( var message: TWMSize );
  52. var
  53.   bResize: boolean;
  54.   xRatio: real;
  55.   i: integer;
  56.   ctl: TWinControl;
  57. begin
  58.   Inc( nCount );
  59.   if Align = alNone then
  60.      bResize := TRUE
  61.   else
  62.      bResize := bResized;
  63.   if not ( csDesigning in ComponentState ) and bResize then
  64.      begin
  65.         if FHorz then
  66.            begin
  67.               xRatio := Width / nOldWidth;
  68.               for i := 0 to ControlCount - 1 do
  69.                  begin
  70.                     ctl := TWinControl( Controls[i] );
  71.                     ctl.Left := Round( ctl.Left * xRatio );
  72.                     ctl.Width := Round( ctl.Width * xRatio );
  73.                  end;
  74.            end;
  75.         if FVert then
  76.            begin
  77.               xRatio := Height / nOldHeight;
  78.               for i := 0 to ControlCount - 1 do
  79.                  begin
  80.                     ctl := TWinControl( Controls[i] );
  81.                     ctl.Top := Round( ctl.Top * xRatio );
  82.                     ctl.Height := Round( ctl.Height * xRatio );
  83.                  end;
  84.            end;
  85.      end
  86.   else
  87.      begin
  88.         nOldWidth := Width;
  89.         nOldHeight := Height;
  90.      end;
  91.   bResized := TRUE;
  92.   nOldWidth := Width;
  93.   nOldHeight := Height;
  94. end;
  95.  
  96. procedure Register;
  97. begin
  98.   RegisterComponents('Additional', [TElasticPanel]);
  99. end;
  100.  
  101. end.
  102.  
  103.